<template>
	<div class="my">
		<view>
			<picker  @change="bindPickerChange" :value="index" :range="types">
				<input class="inputStyle" placeholder="请假类型" disabled v-model="stu.types" />
				<view class="imgRight"></view>
			</picker>
		</view>
		<view>
			<picker mode="date" :value="stu.startDate" :start="startDate" :end="endDate" @change="bindDateChange">
				<input class="inputStyle" placeholder="点击选择开始时间" disabled/>
				<view class="imgRight"></view>
				<view class="myDate">{{stu.startDate}}</view>
			</picker>
		</view>
		<view>
			<picker mode="date" :value="stu.endDate" :start="startDate" :end="endDate" @change="bindDateChange2">
				<input class="inputStyle" placeholder="点击选择结束时间" disabled/>
				<view class="imgRight"></view>
				<view class="myDate">{{stu.endDate}}</view>
			</picker>
		</view>
		<view>
			<input class="inputStyle" placeholder="请假人" v-model="stu.student" />
		</view>
		<view>
			<input class="inputStyle" placeholder="审批人" v-model="stu.teacher" />
		</view>
		<view>
			<textarea class="inputStyle" placeholder="请假原因" v-model="stu.reason"/>
		</view>
		<view>
			<button type="default" form-type="submit" class="loginBtn" @click="login">提交</button>
		</view>
	</div>
</template>

<script>
	const {leave} = require('../../utils/api.js')
	export default {
		data() {
			const currentDate = this.getDate({
				format: true
			})
			return {
				percent:0,
				types: ['事假', '病假', '其他'],
				index: 0,
				stu:{
					types:'',
					startDate: currentDate,
					endDate: currentDate,
					student:'',
					teacher:'',
					reason:''
				},
				id:''
			}
		},
		computed: {
			startDate() {
				return this.getDate('start');
			},
			endDate() {
				return this.getDate('end');
			}
		},
		methods:{
			bindDateChange: function(e) {
				this.stu.startDate = e.target.value
			},
			bindDateChange2: function(e) {
				this.stu.endDate = e.target.value
			},
			getDate(type) {
				const date = new Date();
				let year = date.getFullYear();
				let month = date.getMonth() + 1;
				let day = date.getDate();
				
				if (type === 'start') {
					year = year - 60;
				} else if (type === 'end') {
					year = year + 2;
				}
				month = month > 9 ? month : '0' + month;;
				day = day > 9 ? day : '0' + day;
				return `${year}-${month}-${day}`;
			},
			
			bindPickerChange: function(e) {
				this.index = e.target.value;
				this.stu.types = this.types[this.index];
			},
			login(){
				leave(this.stu).then(data=>{
					uni.showModal({
						content: data.msg,
						showCancel: false
					});
					if(data.code === 200){
						uni.navigateTo({
							url: '../pay/success'
						});
					}
				})
			},
		}
	}
</script>

<style scoped src="../../static/css/my.css"></style>
<style scoped src="../../static/css/service.css"></style>
